package servlets;

import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import java.sql.SQLException;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cliente.CadastroCliente;
import cliente.Cliente;
import endereco.Endereco;

@WebServlet("/cadastrarCliente")
public class CadastrarCliente extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public CadastrarCliente() {
    }
  
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		  CadastroCliente clie = new CadastroCliente();
		  Cliente cliente = new Cliente();
		  Endereco end = new Endereco();
		  
		String nome = request.getParameter("nome");
		String cpf = request.getParameter("cpf");
		String rg = request.getParameter("rg");
		String email = request.getParameter("email");
		String logradouro = request.getParameter("logradouro");
		String numero = request.getParameter("numero");
		String cep = request.getParameter("cep");
		
		cliente.setNome(nome);
		cliente.setCpf(cpf);
		cliente.setRg(rg);
		cliente.setEmail(email);
		end.setLogradouro(logradouro);
		end.setNumero(numero);
		end.setCep(cep);
	
		try {
			clie.InserirCliente(cliente, end);
			Cliente listar = clie.PesquisarPorNome(nome);
			RequestDispatcher rd = request.getRequestDispatcher("clientelistar.jsp");
			request.setAttribute("listas", listar);
			rd.forward(request, response);
		} catch (SQLException e) {
			request.setAttribute("Erro Banco de dados", e.getMessage());
			e.printStackTrace();
		}
		
	}

}
